package beanstao.util.collections;

import java.util.ArrayList;
import java.util.EventListener;
import java.util.EventObject;
import java.util.Vector;

import beanstao.util.collections.interfaces.IWriteValue;

/**
 * 一个当集合内部数据发生CRUD时会触发相应事件的集合类型
 * 
 * @author beanstao@google.com mayh
 * @param <E>
 */
public class EnableEventCollection<E>
{

	private ArrayList<E> _innerList = null;

	@SuppressWarnings("unchecked")
	public E[] getArray()
	{
		ArrayList<E> arrayList = new ArrayList<E>();
		for (int i = 0; i < this._innerList.size(); i++)
			arrayList.add(this._innerList.get(i));
		return (E[]) arrayList.toArray();
	}

	public EnableEventCollection()
	{
		this._innerList = new ArrayList<E>();
	}

	public int size()
	{
		return this._innerList.size();
	}

	public boolean contains(E e)
	{
		return this._innerList.contains(e);
	}

	public E get(int index)
	{
		return this._innerList.get(index);
	}

	public int indexOf(Object o)
	{
		return this._innerList.indexOf(o);
	}

	public int lastIndexOf(Object o)
	{
		return this._innerList.lastIndexOf(o);
	}

	// 有事件add
	public void add(E element)
	{
		this._innerList.add(element);
		this.regCreateEvent(element);
	}

	// 有事件remove
	public E remove(E element)
	{
		E e = null;
		if (this.indexOf(element) != -1)
		{
			e = this._innerList.remove(this.indexOf(element));
			this.regDeleteEvent(element);
		}
		return e;
	}

	// 有事件set
	public E set(E element)
	{

		E e = null;
		if (this.indexOf(element) != -1)
		{
			e = this._innerList.set(this.indexOf(element), element);
			this.regUpdataEvent(element);
		}
		return e;
	}

	/**
	 * 添加Create监听时需要执行的事件集合
	 */
	private final Vector<EventListener> _Createlisteners = new Vector<EventListener>();

	public synchronized void addCreateListener(IWriteValue listener)
	{
		this._Createlisteners.add(listener);
	}

	public synchronized void removeCreateListener(IWriteValue listener)
	{
		this._Createlisteners.remove(listener);
	}

	@SuppressWarnings("unchecked")
	protected void regCreateEvent(E element)
	{
		Vector<EventListener> tempEventListenerVector = null;
		EventObject e = new EventObject(element);

		synchronized (this)
		{
			tempEventListenerVector = (Vector<EventListener>) this._Createlisteners.clone();

			for (EventListener eventListener : tempEventListenerVector)
			{
				IWriteValue writer = (IWriteValue) eventListener;
				writer.WriteValue(e);
			}
		}
	}

	/**
	 * 添加Remove监听时需要执行的事件集合
	 */
	private final Vector<EventListener> _DeleteListener = new Vector<EventListener>();

	public synchronized void addDeleteListener(IWriteValue listener)
	{
		this._DeleteListener.add(listener);
	}

	public synchronized void removeDeleteListener(IWriteValue listener)
	{
		this._DeleteListener.remove(listener);
	}

	@SuppressWarnings("unchecked")
	protected void regDeleteEvent(E element)
	{
		Vector<EventListener> tempEventListenerVector = null;
		EventObject e = new EventObject(element);

		synchronized (this)
		{
			tempEventListenerVector = (Vector<EventListener>) this._DeleteListener.clone();
			for (EventListener eventListener : tempEventListenerVector)
			{
				IWriteValue writer = (IWriteValue) eventListener;
				writer.WriteValue(e);
			}
		}
	}

	/**
	 * 添加Update监听时需要执行的事件集合
	 */
	private final Vector<EventListener> _UpdateListener = new Vector<EventListener>();

	public synchronized void addUpdataListener(IWriteValue listener)
	{
		this._UpdateListener.add(listener);
	}

	public synchronized void removeUpdataListener(IWriteValue listener)
	{
		this._UpdateListener.remove(listener);
	}

	@SuppressWarnings("unchecked")
	protected void regUpdataEvent(E element)
	{
		Vector<EventListener> tempEventListenerVector = null;
		EventObject e = new EventObject(element);

		synchronized (this)
		{
			tempEventListenerVector = (Vector<EventListener>) this._UpdateListener.clone();
			for (EventListener eventListener : tempEventListenerVector)
			{
				IWriteValue writer = (IWriteValue) eventListener;
				writer.WriteValue(e);
			}
		}
	}

}
